/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/23/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringBeanFactory {
	 /**
     * Spring Bean 工厂对象.
     */
    private static SpringBeanFactory beanFactory;

    /**
     * 应用程序上下文对象
     */
    private ApplicationContext context;

    private SpringBeanFactory(ApplicationContext context)
    {
        if(context == null)
        {
            String strPath = "../config/applicationContext.xml";
            this.context = new ClassPathXmlApplicationContext(strPath);
        }
        else
        {
            this.context = context;
        }
    }

    /**
     * 获取Bean工厂对象.
     * 
     * @param context 应用程序上下文.
     * @return SpringBeanFactory
     * @author 王浩
     */
    public static synchronized SpringBeanFactory getInstance(ApplicationContext context)
    {
        if (beanFactory == null)
        {
            beanFactory = new SpringBeanFactory(context);
        }
        
        return beanFactory;
    }
    
    /**
     * 获取Bean工厂对象.
     * 
     * @return SpringBeanFactory
     * @author 王浩
     */
    public static SpringBeanFactory getInstance()
    {
        return getInstance(null);
    }

    /**
     * 通过bean的ID获取到Spring管理的Bean对象
     * 
     * @param beanId
     * @return Object
     * @author 王浩
     */
    public Object getBean(String beanId)
    {
        return context.getBean(beanId);
    }
}
